package com.ares.aler.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.ares.aler.service.UserDao;
import com.ares.aler.utilPackage.aliDuanxin.SmsDemo;
import com.ares.aler.entiy.MsgPo;
import com.ares.aler.entiy.UserPo;
import com.ares.aler.mapper.MsgMapperDao;
import com.ares.aler.mapper.UserMapperDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserDaoImpl implements UserDao {

    @Autowired
    UserMapperDao userMapperDao;

    @Autowired
    MsgMapperDao msgMapperDao;

    private static final Logger log = LoggerFactory.getLogger(UserDaoImpl.class);

    @Override
    public UserPo selectById(String id) {
        UserPo userPo = userMapperDao.selectoneByKey("1");
        String password = userPo.getPassword();
        log.info("------------我是 password-----------"+password);
        return userPo;
    }

    @Override
    public Map selectAllMap(int page,int limit)  {
        Map userMap = new HashMap();
        page = (page - 1)*limit;
        List<UserPo> userPoList = userMapperDao.selectAllList(page,limit);
        int count = userMapperDao.selectCount();
        userMap.put("data",userPoList);
        userMap.put("code",0);
        userMap.put("count",count);
        userMap.put("msg","");
        return userMap;
    }

    @Override
    public void birthdaySelectMsg() {
        List<UserPo> userPoList = userMapperDao.selectAll();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = sdf.format(date);
        for (UserPo userPo :userPoList){
            if(nowDate.equals(userPo.getShengri())){
                SmsDemo sd = new SmsDemo();
                try {
                    MsgPo msgPo = sd.sendMsg(userPo.getUsername(),userPo.getTel());
                    msgPo.setId(String.valueOf(UUID.randomUUID()));
                    msgMapperDao.seveMsgPo(msgPo);
                } catch (ClientException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public int seveUserPo(UserPo userPo) {
        userPo.setId(String.valueOf(UUID.randomUUID()));
        return userMapperDao.seveUserPo(userPo);
    }


}
